#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct TREE_NODE TreeNode;
typedef int Indice;
typedef TreeNode* Element;
typedef struct QUEUE_NODE QueueNode;


struct TREE_NODE {
	Indice idc;
	TreeNode *left;
	TreeNode *right;
	TreeNode *father;
};

struct QUEUE_NODE {
	Element elm;
	QueueNode *next;
};

QueueNode* initQueue();
_Bool queueEmpty(QueueNode*);
void inQueue(QueueNode*,Element);
Element outQueue(QueueNode*);
_Bool isLeaf(TreeNode *);

int main(void){
	//		init 10 tree nodes;
	Element nodePtr[10];
	for(int i=0;i<10;i++){
		nodePtr[i]=malloc(sizeof(TreeNode));
		nodePtr[i]->idc=i;
		nodePtr[i]->left=nodePtr[i]->right=nodePtr[i]->father=NULL;
	}

	// 		read data
	int nodes,l_idc,r_idc;
	char l,r,drop;
	scanf("%d",&nodes);
	while( (drop=getc(stdin)) != '\n' );
	for(int i=0;i<nodes;i++){
		scanf("%c %c",&l,&r);
		while( (drop=getc(stdin)) != '\n' );
		if( l != '-' ){
			l_idc= l- '0';
			nodePtr[i]->left= nodePtr[l_idc];
			nodePtr[l_idc]->father=nodePtr[i];
		}else{
			nodePtr[i]->left=NULL;
		}
		if( r != '-' ){
			r_idc= r-'0';
			nodePtr[i]->right=nodePtr[r_idc];
			nodePtr[r_idc]->father=nodePtr[i];
		}else{
			nodePtr[i]->right=NULL;
		}
	}
	Element root=nodePtr[0];
	while( root->father )
		root=root->father;
	
	QueueNode* q=initQueue();
	inQueue(q,root);
	Element tmp;
	_Bool firstChar=true;
	while ( !queueEmpty(q) ){
		tmp=outQueue(q);
		if ( isLeaf(tmp) ){
			if( firstChar ){
				printf("%d",tmp->idc);
				firstChar=false;
			}else 
				printf(" %d",tmp->idc);
		}
		if(tmp->left)
			inQueue(q,tmp->left);
		if(tmp->right)
			inQueue(q,tmp->right);
	}
}


QueueNode* initQueue(){
	QueueNode *head=malloc(sizeof(QueueNode));
	head->next=NULL;
	return head;
}

_Bool queueEmpty(QueueNode *head){
	if( !(head->next) )
		return true;
	return false;
}

void inQueue(QueueNode *head,Element new_elm){
	while( head->next ){
		head=head->next;
	}
	QueueNode *new_node=malloc(sizeof(QueueNode));
	new_node->elm=new_elm;
	new_node->next=NULL;
	head->next=new_node;
	return;
}

Element outQueue(QueueNode *head){
	Element out_elm=head->next->elm;
	QueueNode *tmp=head->next->next;
	free(head->next);
	head->next=tmp;
	return out_elm;
}

_Bool isLeaf(TreeNode *head){
	if( !(head->left) && !(head->right) )
		return true;
	else
		return false;
}
